package pac1_rgr;

import process.Actor;
import process.IWaitCondition;
import queues.QueueForTransactions;
import queues.Store;

public class Barge extends Actor {
	protected int size;
	private QueueForTransactions queueOfBarges;
	private QueueForTransactions queueOfContainers;
	private QueueForTransactions queuePort;
	private Main gui;
	private Model model;

	public Barge(String name, Main gui, Model model) {
		this.setNameForProtocol(name);
		this.gui = gui;
		this.model = model;
		this.queueOfBarges = model.getQueueOfBarges();
		this.queuePort = model.getQueuePort();
		this.size = gui.getChooseDataBargeSize().getInt();
		this.queueOfContainers=getQueueOfContainers();

	}

	@Override
	protected void rule() {
		// Цикл правил дії баржi
		IWaitCondition empty = new IWaitCondition() {

			@Override
			public boolean testCondition() {
				return (queueOfContainers.queue.size() == 0);
			}
			public String toString(){
				return "розвантаження";
				
			}
		};
		queueOfBarges.addLast(this);
		for (int i = 0; i < size; i++) {
			this.queueOfContainers.add(getDispatcher().getCurrentTime());
			queuePort.add(1);
		}
		// Чекає поки развантажиться
		try {
			waitForCondition(empty);
		} catch (Exception e) {
			return;
		}
		queueOfBarges.remove(this);
		getDispatcher().printToProtocol(this.getNameForProtocol() + " пiшла");
	}

	public boolean isEmpty() {
		return this.queueOfContainers.queue.size() == 0;
	}

	public Object removeContainer() {
		return queueOfContainers.removeFirst();
	}

	public int getSize() {
		return this.queueOfContainers.queue.size();
	}
	public QueueForTransactions getQueueOfContainers() {
		if (queueOfContainers == null) {
			queueOfContainers = new QueueForTransactions();
			queueOfContainers.setNameForProtocol("Черга контейнерiв");
			queueOfContainers.setDispatcher(model.getDispatcher());
		}
		return queueOfContainers;
	}
}
